package com.gateway.api.domain.web;


import com.gateway.api.domain.contant.ReturnEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

@Data
public class ReturnInfo<T> implements Serializable {
    private String code;
    private String msg;
    private T result;
    private Map<String,Object> ext;

    private ReturnInfo() {
    }
    public static <T> ReturnInfo getInstance(String code,String msg,T result,Map<String,Object> ext){
        ReturnInfo returnInfo = new ReturnInfo();
        returnInfo.setCode(code);
        returnInfo.setMsg(msg);
        returnInfo.setResult(result);
        returnInfo.setExt(ext);
        return returnInfo;
    }
    public static <T> ReturnInfo success(T result, Map<String,Object> ext){
        return getInstance(ReturnEnum.SUCCESS.getCode(),ReturnEnum.SUCCESS.getMsg(),result,ext);
    }
    public static <T> ReturnInfo success(T result){
        return success(result,null);
    }
    public static ReturnInfo defaultError(){
        return error(ReturnEnum.DEFAULT_ERROR.getCode(),ReturnEnum.DEFAULT_ERROR.getMsg());
    }
    public static ReturnInfo error(String code , String msg){
        return getInstance(code,msg,null,null);
    }
    public static ReturnInfo getSimpleResultByEnum(ReturnEnum returnEnum){
        return getInstance(returnEnum.getCode(),returnEnum.getMsg(),null,null);
    }
    public boolean getSuccess(){
        return "0".equals(code);
    }

}
